<?php

/**
 * Created by EPP.
 * User: yuxun
 * Date: 18/6/23
 * Time: 下午4:44
 */
class ChannelController extends AdminBaseController {

	public function index(){
		$this->setTitle('渠道管理');
	
		$filter = new PubFilter();
		$grid   = new PubGrid();
	
		$filter->addFilter('channel_name', 'input', '渠道名称');
        $filter->addSpecial(true);
	
		$params = $filter->get();
		$page = $this->R('page', '1');
		$m = $this->R('m');
		$params['m'] = $m;
		$header = ChannelService::indexHeader();
		if(PubFunc::isAjax()){
			$result = ChannelService::indexData($params);
			$this->out = array(
					'count'     => $result['count'],
					'data'      => $result['data'],
					'result'    => true
			);
			$this->outType = 'json';
		}else{
			$order  = 'create_time';
			$url = PubFunc::ajaxUrl("?r=channel/index", $params);
			$grid->addGrid($header, $url, array(
					'name'=>'数据',
					'order'=>array($order, 'desc'),
					'type'=>'channel/index',
					'length'=>50,
					'page'=>$page
			));
	
			$this->out['filter'] = $filter->show();
			$this->out['grid'] = $grid->show();
	
		}
	}
	

    public function add(){
        $this->setTitle('新建渠道');
        
        $specialList = SpecialServerService::specialList();
        $this->out['specialList'] = $specialList;
    }

    public function doAdd(){
    	$count = ChannelService::crepeat(0, 'tab', $this->R('tab'),$this->R('g'));
    	if($count > 0){
    		$this->showMessage('渠道ID已经存在', PubFunc::buildUrl("/oss/?r=channel/index"));
    	}else{
    		$result = ChannelService::doAdd($_POST);
    		
    		if($result !== false){
    			$this->showMessage('新增成功', PubFunc::buildUrl("/oss/?r=channel/index"));
    		}else{
    			$this->showMessage('新增失败');
    		}
    	}
    	
        
    }

    public function edit(){
        $id = $this->R('id');
        $info = ChannelService::get($id);
        
        if(empty($info)) $this->showMessage('找不到该信息');
        
        $modSpecialChannel = new ModSpecialChannel();
        $obj = $modSpecialChannel->findAllByAttributes(array('channel_id' => $id));
        $special_ids = '';
        $special_ids_array = array();
        foreach ($obj as $key=>$val){
        	$special_ids .= ','.$val->special_id;
        	$special_ids_array[] = $val->special_id;
        }
        $special_ids = substr($special_ids, 1);
        $info['special_id'] = $special_ids;
        $this->out['special_ids_array'] = $special_ids_array;

        $this->setTitle('编辑渠道', $info['channel_name']);

        $this->out['info'] = $info;
        
        $specialList = SpecialServerService::specialList();
        $this->out['specialList'] = $specialList;
        
        $backUrl = PubFunc::buildBackUrl("/oss/?r=channel/index");  //返回路径
        $this->out['backUrl'] = $backUrl;
        
        $page = $this->R('page');
        $this->out['page'] = $page;
    }

    public function doEdit(){
        $id = $this->R('id');
        $count = ChannelService::crepeat($this->R('id'), 'tab', $this->R('tab'),$this->R('g'));
        if($count > 0){
        	$this->showMessage('渠道ID已经存在', PubFunc::buildUrl("/oss/?r=channel/index"));
        }else{
        	$result = ChannelService::doEdit($id, $_POST);
        	
        	if($result !== false){
        		$this->showMessage('编辑成功', PubFunc::buildBackUrl("/oss/?r=channel/index"));
        	}else{
        		$this->showMessage('编辑失败');
        	}
        }
        
    }

    /*
     * 删除渠道时，判断是否服务器已经使用了该渠道（有则不能删除），无则也需删除对应的专服关系
     */
    public function delete(){
        $id = $this->R('id');
        
        $modServerChannel = new ModServerChannel();
        $result_a = $modServerChannel->getSelectServer(array("channel_id"=>$id));
        if(count($result_a) > 0){
        	$this->showMessage('该渠道下存在服务器，不能进行删除', PubFunc::buildBackUrl("/oss/?r=channel/index"));
        }else{
        	$result = ChannelService::delete($id);
        	
        	if($result !== false){
        		$this->showMessage('删除成功', PubFunc::buildBackUrl("/oss/?r=channel/index"));
        	}else{
        		$this->showMessage('删除失败', PubFunc::buildBackUrl("/oss/?r=channel/index"));
        	}
        }

        
    }
    
    /**
     * 对应服务器
     */
    public function setServer(){
    	$this->setTitle('配置服务器');
    	 
    	$platform = PubBase::getCurrentPlatform();
    	$game = PubBase::getCurrentGame();
    	$serverlList = PubServer::getOptions($platform,$game,'id');
    	$this->out['serverlList'] = $serverlList;
    	 
    	$id = $this->R('id');
    	$this->out['id'] = $id;
    	 
    	//查询已经设置的渠道
    	$already = ChannelService::alreadyServer($id);
    	$this->out['already'] = $already;
    }
    
    public function doServer(){
    	//先删除原来的，再重新插入新的
    	ChannelService::batchDelete($_POST['channel_id']);
    	 
    	$result = ChannelService::doChannel($_POST);

    	if($result['state']){
    		$this->showMessage('设置成功', PubFunc::buildUrl("/oss/?r=channel/index"));
    	}else{
    		$this->showMessage($result['msg']);
    	}
    }
    
    /**
     * 根据渠道获取服务器
     */
    public function getServer(){
    	$os = PubRequest::R('os');
    	$g = PubRequest::R('g');
    	if($os == 'ALL'){
    		//渠道为all时，查询所有渠道，即查询当前专服下的所有服务器
    		$special_id = PubRequest::R('special_id');
    		$game = PubBase::getCurrentGame();
    		if($special_id){
    			$list = ServerService::getServerForSpecialId($g,$special_id);
    		}else{
    			$platform = PubBase::getCurrentPlatform();
    			$modServer = new ModServer();
    			$list = $modServer->select(array(
    					'p' => $platform,
    					'g' => $game
    			));
    		}
	
    	}else if($os == ''){
    		$list = array();
    	}else{
    		$list = ChannelService::getServer($os,$g);
    	}
    	
    	$data = array();
    	$data['list'] = $list;  	
    	$user = AccAuth::me();
    	if($user['IsViewAll'] == 1){
    		$data['IsViewAll'] = 1;
    	}else{
    		$data['IsViewAll'] = 2;
    	}
    	$this->out = array(
    			'status' => 1,
    			'msg' => '操作成功',
    			'data'    => $data
    	);
    	$this->outType = 'json';
    }
    
    /**
     * 根据专服获取渠道
     */
    public function getChannelForSpecial(){
    	$special_id = PubRequest::R('special_id');
    	if($special_id == ''){
    		$list = array();
    	}else{
    		$list = ChannelService::getChannelForSpecial($special_id);
    	}
    	
    	
    	$data = array();
    	$data['list'] = $list;
    	$user = AccAuth::me();
    	if($user['IsViewAll'] == 1){
    		$data['IsViewAll'] = 1;
    	}else{
    		$data['IsViewAll'] = 2;
    	}
    	
    	$this->out = array(
    			'status' => 1,
    			'msg' => '操作成功',
    			'data'    => $data
    	);
    	$this->outType = 'json';
    }
    
    /**
     * 根据专服获取服务器
     */
    public function getServerForSpecial(){
    	$special_id = PubRequest::R('special_id');
    	if($special_id == ''){
    		$list = array();
    	}else{
    		$list = SpecialServerService::getServer($special_id);
    	}

    	$this->out = $list;
    	$this->outType = 'json';
    }
    
    
    public function searchChannel(){
    	$this->layout = '';
    	$game = PubBase::getCurrentGame();
    	
    	
    	$channel_ids = PubRequest::R('channel_ids');
    	
    	$modChannel = new ModChannel();
    	$list = $modChannel->searchChannel($game,$channel_ids);
    	 
    	$this->out['list'] = $list;
    }

}